/*
 * init_cpp.c
 *
 *  Created on: 2012/08/01
 *      Author: owner
 */

#include <stdio.h>
#include "sqlite3.h"
#include "typedef.h"
#include "db_def.h"
#include "core.h"
#include "Id.h"

/*TBL_CPPDIRECTIVEINFO*/
static INT createCppDirectiveInfo(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q TEXT, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_CPPDIRECTIVEINFO, _T_IDX(3), _T_IDX(4), _T_IDX(5),
					_T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_CPPMACRODEFINFO*/
static INT createCppMacroDefInfo(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q TEXT,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_CPPMACRODEFINFO, _T_IDX(3), _T_IDX(4), _T_IDX(5),
					_T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9), _T_IDX(10));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_CPPDEFREPINFO*/
static INT createCppDefRepInfo(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q TEXT, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_CPPDEFREPINFO, _T_IDX(3), _T_IDX(4 ), _T_IDX(5),
					_T_IDX(6), _T_IDX(7), _T_IDX(8));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_CPPPPCONDITION*/
static INT createCppPPCondition(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q DOUBLE, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_CPPPPCONDITION, _T_IDX(3), _T_IDX(4), _T_IDX(5),
					_T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9), _T_IDX(10));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_CPPTOKENLIST*/
static INT createCppTokenList(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q TEXT, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_CPPTOKENLIST, _T_IDX(3), _T_IDX(4), _T_IDX(5 ),
					_T_IDX(6 ));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*initialize cpp db*/
INT DLL_EXPORT initCppDB(sqlite3 * coreData) {
	INT rc = 0;

	rc = createCppDirectiveInfo(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createCppMacroDefInfo(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createCppDefRepInfo(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createCppPPCondition(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createCppTokenList(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	return COEXIT_SUCCESS;
}
